local chenpin = fk.CreateSkill{
  name = "investiture__chenpin",
}

Fk:loadTranslationTable{
  ["investiture__chenpin"] = "晨牝",
  [":investiture__chenpin"] = "出牌阶段，你可将性别变更为女，然后交给一名男性角色至多三张手牌并对其视为使用一张<a href=':fire__honey_trap'>火【美人计】</a>。" ..
  "若因此受伤的角色：<br/>为你，你弃置所有手牌并结束回合，然后获得两张【金蝉脱壳】；<br/>为目标角色，你获得其X张牌（X为你与其手牌的差值）。",

  ["#investiture__chenpin"] = "晨牝：你可将性别变更为女",
  ["#investiture__chenpin-use"] = "晨牝：对一名角色视为使用一张火【美人计】",
  ["#investiture__chenpin-give"] = "晨牝：交给 %src 至多三张手牌",
  ["#investiture__chenpin-prey"] = "晨牝：选择 %src 的%arg张牌获得",

  ["$investiture__chenpin1"] = "燃心魂焰！",
  ["$investiture__chenpin2"] = "灵狐惑心~",
}

---@param room Room
---@param start integer
---@return ServerPlayer?
local function getSuffered(room, start)
  local all_events = room.logic.all_game_events
  for i = start, #all_events do
    if all_events[i].event == GameEvent.Damage then
      local data = all_events[i].data ---@type DamageData
      if data.card and data.card.skillName == chenpin.name and data.damage > 0 then
        return data.to
      end
    end
  end
end

chenpin:addEffect("active", {
  anim_type = "special",
  prompt = "#investiture__chenpin",
  card_num = 0,
  target_num = 0,
  can_use = function(self, player)
    return player.gender ~= General.Female
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    room:setPlayerProperty(effect.from, "gender", General.Female)
    local use = room:askToUseVirtualCard(effect.from, {
      name = "fire__honey_trap",
      skill_name = chenpin.name,
      cancelable = false,
      skip = true,
      prompt = "#investiture__chenpin-use",
    })
    room:askToYiji(effect.from, {
      min_num = 0,
      max_num = 3,
      cards = effect.from:getCardIds("h"),
      targets = use.tos,
      skill_name = chenpin.name,
      cancelable = false,
      prompt = "#investiture__chenpin-give:" .. use.tos[1].id,
    })
    if effect.from.dead or table.find(use.tos, function(p) return p.dead end) then return end
    room:useCard(use)
    if effect.from.dead then return end
    local suffered = getSuffered(room, room.logic:getCurrentEvent().id)
    if not suffered then return end
    if suffered == effect.from then
      room:throwCard(suffered:getCardIds("h"), chenpin.name, suffered, suffered)
      if effect.from:isAlive() then
        room:endTurn()
        local cards = table.filter(room.void, function (id)
          return Fk:getCardById(id).name == "crafty_escape"
        end)
        if #cards > 2 then
          cards = table.slice(cards, 1, 3)
        else
          while #cards < 2 do
            table.insert(cards, room:printCard("crafty_escape", Card.Heart, 1).id)
          end
        end
        room:obtainCard(effect.from, cards, true, fk.ReasonJustMove, effect.from, chenpin.name, MarkEnum.DestructIntoDiscard)
      end
    end
    table.forEach(use.tos, function (p)
      if suffered == p then
        local num = math.abs(p:getHandcardNum() - effect.from:getHandcardNum())
        if num > 0 then
          local cards = room:askToChooseCards(effect.from, {
            min = num,
            max = num,
            flag = "h",
            target = p,
            skill_name = chenpin.name,
            prompt = "#investiture__chenpin-prey:"..p.id.."::"..num,
          })
          room:obtainCard(effect.from, cards, false, fk.ReasonPrey, effect.from, chenpin.name)
        end
      end
    end)
  end
})

return chenpin